package com.example.jh352160.surfingtv.Index.customView;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.Index.MainActivity;
import com.example.jh352160.surfingtv.Index.customView.adapter.BannerPagerAdapter;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.IndexItem;
import com.example.jh352160.surfingtv.search.SearchPage;
import com.example.jh352160.surfingtv.thematic_details.ThematicDetailsPage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jh352160 on 2016/7/19
 */

public class Banner extends RelativeLayout {

    Context context;
    ViewPager viewPager;
    ImageButton searchButton;
    BannerPagerAdapter bannerPagerAdapter;

    public Banner(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public Banner(Context context, AttributeSet attr) {
        super(context, attr);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.index_banner, this);
        viewPager = (ViewPager) findViewById(R.id.bannerViewPager);
        final List<ImageView> imageViews = new ArrayList<>();
        searchButton = (ImageButton) findViewById(R.id.searchButton);
        int[] dotId = {R.id.v_dot1, R.id.v_dot2, R.id.v_dot3, R.id.v_dot4};
        final List<View> dotViews = new ArrayList<>();
        for (int i : dotId) {
            dotViews.add(findViewById(i));
        }

        //api
        final IndexItem.DataBean data = MainActivity.getData();
        imageViews.clear();
        for (int i = 0; i < 4; i++) {
            final int j = i;
            String url = data.getBanner().get(i).getImageUrl();
            ImageView imageView = new ImageView(context);
            Glide.with(context).load(url).into(imageView);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ThematicDetailsPage.class);
                    intent.putExtra("contentId", data.getBanner().get(j).getContentId());
                    context.startActivity(intent);
                }
            });
            imageViews.add(imageView);
        }

//        int[] imageId={R.drawable.banner_image1,R.drawable.banner_image2,R.drawable.banner_image3};
//        for(int i=0;i<4;i++){
//            ImageView image=new ImageView(context);
//            image.setBackgroundColor(getResources().getColor(android.R.color.white));
//            imageViews.add(image);
//        }
        bannerPagerAdapter = new BannerPagerAdapter(imageViews);
        viewPager.setAdapter(bannerPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (View i : dotViews) {
                    i.setBackgroundColor(getResources().getColor(R.color.dotColor_unselected));
                }
                dotViews.get(position).setBackgroundColor(getResources().getColor(R.color.dotColor));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        searchButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, SearchPage.class);
                context.startActivity(intent);
            }
        });
    }

}
